package ac.github.os.internal.core.action.entity

import ac.github.os.internal.core.action.IEntityAction
import ac.github.os.internal.core.config.StringConfig
import ac.github.os.internal.core.config.get
import ac.github.os.internal.session.Cache
import ac.github.os.internal.session.Session
import org.bukkit.entity.LivingEntity
import taboolib.common.platform.Awake

abstract class VectorAction : IEntityAction {
    override fun onExecute(session: Session, cache: Cache, config: StringConfig) {
        run(config,cache.filterIsEntities())
    }

    override val namespace: String
        get() = "entity:vector:$name"

    abstract val name: String

    abstract fun run(config: StringConfig, entities: List<LivingEntity>)

    fun StringConfig.value() : Double? =  get<Double>("val", single = true)

    fun StringConfig.pitch() : Float? =  get<Float>("pitch")

}

/**
 * pitch p
 */
@Awake
class VectorMultiply : VectorAction() {

    override val name: String
        get() = "multiply"

    override fun run(config: StringConfig, entities: List<LivingEntity>) {
        val d = config.value() ?: return
        val pitch = config.pitch()
        val x = config.get<Double>("x")
        val y = config.get<Double>("y")
        val z = config.get<Double>("z")
        entities.forEach {
            val location = it.location.clone()
            pitch?.let { location.pitch = it }
            val direction = location.direction
            direction.multiply(d)

            x?.let { direction.x = it }
            y?.let { direction.y = it }
            z?.let { direction.z = it }

            it.velocity = direction
        }
    }
}
